package com.ymatou.userservice.testcase;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.userservice.parameter.WeiXinUserRequestDtoBean;
import com.ymatou.userservice.parameter.WeiXinUserUnionYmatouUserRequestDtoBean;
import com.ymatou.userservice.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.userservice.service.WeiXinUserUnionYmatouUserRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.common.enums.ChannelSourceEnum;
import com.ymttest.common.enums.UserTerminalSourceEnum;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
*用户微信账号关联其码头账号接口
*/
@TestSuite
public class Ts_WeiXinUserUnionYmatouUserRequestDto {

	private static WeiXinUserUnionYmatouUserRequestDtoBean weixinuserunionymatouuserrequestdtobean;
	private static WeiXinUserUnionYmatouUserRequestDtoCall weixinuserunionymatouuserrequestdtocall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户微信账号关联其码头账号接口");
	}

	@Before
	public void caseUp() {
		weixinuserunionymatouuserrequestdtobean=new WeiXinUserUnionYmatouUserRequestDtoBean();
		weixinuserunionymatouuserrequestdtocall=new WeiXinUserUnionYmatouUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(weixinuserunionymatouuserrequestdtocall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_WeiXinUserUnionYmatouUserRequestDto_001(){
		Logger.start(true,"微信关联洋码头账号成功");
		try {
			WeiXinUserRequestDtoBean weixinuserrequestdtoBean = createWeiXinUser(TestDataManager.getRandomNum(6), TestDataManager.getRandomNum(7));
			weixinuserunionymatouuserrequestdtobean.setWeiXinUser(weixinuserrequestdtoBean);
			weixinuserunionymatouuserrequestdtobean.setUserId(registerNewUser());
			weixinuserunionymatouuserrequestdtocall.setData(weixinuserunionymatouuserrequestdtobean);
			weixinuserunionymatouuserrequestdtocall.callService();
			Logger.verifyEquals("true", weixinuserunionymatouuserrequestdtocall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.SUCCESS, weixinuserunionymatouuserrequestdtocall.getString("ResponseCode"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_WeiXinUserUnionYmatouUserRequestDto_002(){
		Logger.start(false,"微信已关联过洋码头账号，关联失败");
		try {
			WeiXinUserRequestDtoBean weixinuserrequestdtoBean = createWeiXinUser("767529", "1064650");
			weixinuserunionymatouuserrequestdtobean.setWeiXinUser(weixinuserrequestdtoBean);
			weixinuserunionymatouuserrequestdtobean.setUserId(registerNewUser());
			weixinuserunionymatouuserrequestdtocall.setData(weixinuserunionymatouuserrequestdtobean);
			weixinuserunionymatouuserrequestdtocall.callService();
			Logger.verifyEquals(ECode.ERROR400, weixinuserunionymatouuserrequestdtocall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", weixinuserunionymatouuserrequestdtocall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("已关联过码头用户", weixinuserunionymatouuserrequestdtocall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiXinUserUnionYmatouUserRequestDto_003(){
		Logger.start(false,"不输入关联Id,失败");
		try {
			WeiXinUserRequestDtoBean weixinuserrequestdtoBean = createWeiXinUser(null, TestDataManager.getRandomNum(7));
			weixinuserunionymatouuserrequestdtobean.setWeiXinUser(weixinuserrequestdtoBean);
			weixinuserunionymatouuserrequestdtobean.setUserId(registerNewUser());
			weixinuserunionymatouuserrequestdtocall.setData(weixinuserunionymatouuserrequestdtobean);
			weixinuserunionymatouuserrequestdtocall.callService();
			Logger.verifyEquals(ECode.ERROR400, weixinuserunionymatouuserrequestdtocall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", weixinuserunionymatouuserrequestdtocall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("微信unionid不能为空", weixinuserunionymatouuserrequestdtocall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiXinUserUnionYmatouUserRequestDto_004(){
		Logger.start(false,"不输入openId,失败");
		try {
			WeiXinUserRequestDtoBean weixinuserrequestdtoBean = createWeiXinUser(TestDataManager.getRandomNum(6), null);
			weixinuserunionymatouuserrequestdtobean.setWeiXinUser(weixinuserrequestdtoBean);
			weixinuserunionymatouuserrequestdtobean.setUserId(registerNewUser());
			weixinuserunionymatouuserrequestdtocall.setData(weixinuserunionymatouuserrequestdtobean);
			weixinuserunionymatouuserrequestdtocall.callService();
			Logger.verifyEquals(ECode.ERROR400, weixinuserunionymatouuserrequestdtocall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", weixinuserunionymatouuserrequestdtocall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("微信OpenId不能为空", weixinuserunionymatouuserrequestdtocall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiXinUserUnionYmatouUserRequestDto_005(){
		Logger.start(false,"不输入用户Id,失败");
		try {
			WeiXinUserRequestDtoBean weixinuserrequestdtoBean = createWeiXinUser(TestDataManager.getRandomNum(6), TestDataManager.getRandomNum(7));
			weixinuserunionymatouuserrequestdtobean.setWeiXinUser(weixinuserrequestdtoBean);
			weixinuserunionymatouuserrequestdtocall.setData(weixinuserunionymatouuserrequestdtobean);
			weixinuserunionymatouuserrequestdtocall.callService();
			Logger.verifyEquals(ECode.ERROR400, weixinuserunionymatouuserrequestdtocall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", weixinuserunionymatouuserrequestdtocall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("用户未找到", weixinuserunionymatouuserrequestdtocall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_WeiXinUserUnionYmatouUserRequestDto_006(){
		Logger.start(true,"微信老用户数据修复JIRACONN-7439");
		try {
			WeiXinUserRequestDtoBean weixinuserrequestdtoBean = createWeiXinUser(TestDataManager.getRandomNum(6), TestDataManager.getRandomNum(7));
			weixinuserunionymatouuserrequestdtobean.setWeiXinUser(weixinuserrequestdtoBean);
			weixinuserunionymatouuserrequestdtobean.setUserId(registerNewUser());
			weixinuserunionymatouuserrequestdtocall.setData(weixinuserunionymatouuserrequestdtobean);
			weixinuserunionymatouuserrequestdtocall.callService();
			Logger.verifyEquals("true", weixinuserunionymatouuserrequestdtocall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.SUCCESS, weixinuserunionymatouuserrequestdtocall.getString("ResponseCode"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * 创建微信用户
	 * @return
	 */
	private WeiXinUserRequestDtoBean createWeiXinUser(String unionId, String OpenId){
		Logger.comment("创建一个新的微信账号");
		WeiXinUserRequestDtoBean weixinuserrequestdtoBean = new WeiXinUserRequestDtoBean();
		weixinuserrequestdtoBean.setUnionId(unionId);
		weixinuserrequestdtoBean.setOId(OpenId);
		weixinuserrequestdtoBean.setName("Meme");
		weixinuserrequestdtoBean.setSex("Femal");
		weixinuserrequestdtoBean.setProvince("ShangHai");
		weixinuserrequestdtoBean.setCity("Shanghai");
		weixinuserrequestdtoBean.setRegSource("App");
		weixinuserrequestdtoBean.setTSource("Pc");
		return weixinuserrequestdtoBean;
	}
	
	/**
	 * 注册新用户便于测试
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private int registerNewUser() throws ClientProtocolException, IOException{
		Logger.comment("创建一个新的洋码头账号");
		UserRegisterInfoRequestDtoBean userBean = new UserRegisterInfoRequestDtoBean();
		UserRegisterInfoRequestDtoCall userCall = new UserRegisterInfoRequestDtoCall();
		String loginId = "autot_" + TestDataManager.randomStr(6);
		String loginMail = loginId + "@ymatou.com";
		String loginMobile = TestDataManager.getPhoneNum();
		String pas = "123456";
		int registerType = 0;
		Boolean isAgreeContract = true;
		int regSource = 0;
		int visitLan = 0;
		ChannelSourceEnum channelSource = ChannelSourceEnum.Alipay;
		UserTerminalSourceEnum terminalSource = UserTerminalSourceEnum.Android;
		String clientIp = "127.0.0.1";
		Long tokenTimeOut = (long) 1;

		userBean.setLoginId(loginId);
		userBean.setLoginMail(loginMail);
		userBean.setLoginMobile(loginMobile);
		userBean.setPas(pas);
		userBean.setRegisterType(registerType);
		userBean.setIsAgreeContract(isAgreeContract);
		userBean.setRegSource(regSource);
		userBean.setVisitLan(visitLan);
		userBean.setChannelSource(channelSource);
		userBean.setTerminalSource(terminalSource);
		userBean.setClientIp(clientIp);
		userBean.setTokenTimeOut(tokenTimeOut);

		userCall.setData(false, userBean);
		userCall.callService();
		return userCall.getUserid();
	}
}
